package com.example.phonefinder;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class ChildFinder extends Activity implements OnClickListener{
	
	private EditText username_;
	private EditText password_;
	private Button login_;
	private Button createAccount_;
	public static final String loginFile = "LoginFile";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_childfinder);
	        
	        username_ = (EditText) findViewById(R.id.username);
	        password_ = (EditText) findViewById(R.id.password);
			login_ = (Button) findViewById(R.id.login);
			createAccount_ = (Button) findViewById(R.id.createAccount);
			
			login_.setOnClickListener(this);
			createAccount_.setOnClickListener(this);
			
			// Restore preferences
			SharedPreferences settings = getSharedPreferences(loginFile, 0);
			String username = settings.getString("Username", "");
			String password = settings.getString("Username", "");
			boolean check = settings.getBoolean("Exists", false);
			
			System.out.println(username +"," + password + "," + check);
			if(check)
				createAccount_.setEnabled(false);
				
			username_.setText(username);
	}


	@Override
	protected void onStop(){
		super.onStop();
		
		System.out.println("called onStop()");

		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(loginFile, 0);
		String username = settings.getString("Username", "");
		boolean check = settings.getBoolean("Exists", false);
		
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("Username", username);
		editor.putBoolean("Exists", check);

		// Commit the edits!
		editor.commit();
		
		// Commit valid username to screen
		username_.setText(username);
		password_.setText("");
		if(check)
			createAccount_.setEnabled(false);
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.activity_settings, menu);
	        return true;
    }

	@Override
	public void onClick(View v) {
		SharedPreferences settings = getSharedPreferences(loginFile, 0);
		String user, pass;
		Toast tag;
		
		//Switch statement to pick correct onClickListener
		switch(v.getId()) {
        case R.id.login: // Checks valid login and launches rule activity
        	user = settings.getString("Username", "");
        	pass = settings.getString("Password", "");
        	System.out.println(user +"," +pass);
        	if(user.equals("") || pass.equals("") ){
        		tag = Toast.makeText(this, "Invalid Username or Password!", Toast.LENGTH_LONG);
        		tag.show();
        	}
        	else if(!user.equals(username_.getText().toString()) || !pass.equals(password_.getText().toString())){
        		tag = Toast.makeText(this, "Invalid Username or Password!", Toast.LENGTH_LONG);
        		tag.show();
        	}
        	else {
        		tag = Toast.makeText(this, "Login Successful!", Toast.LENGTH_LONG);
        		tag.show();
        		//Intent i = new Intent(ChildFinder.this,bensclass.class);
    			//startActivity(i);
        	}
        break;
        case R.id.createAccount: // Saves new account to file
        	SharedPreferences.Editor editor = settings.edit();
        	editor.putString("Username", username_.getText().toString());
    		editor.putString("Password", password_.getText().toString());
    		editor.putBoolean("Exists", true);
    		username_.getText().toString();
    		editor.commit();
    		createAccount_.setEnabled(false);
    		
    		tag = Toast.makeText(this, "Account has been Created!", Toast.LENGTH_LONG);
    		tag.show();
        break;
		}
	}
}
